#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/CashFlow.h>
#pragma unmanaged 
#include <ql\cashflows\dividend.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Cashflows {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDividend
	public ref class CDividend : 
            public CCashFlow,
            public Cephei::QL::Cashflows::IDividend
	{
	protected: 
		boost::shared_ptr<QuantLib::Dividend>* _ppDividend;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Dividend>* _phDividend;
#endif
		Object^ _DividendOwner;     // reference to object that manages the storage for this object
	internal:
        CDividend (boost::shared_ptr<QuantLib::Dividend>& childNative, Object^ owner);
        CDividend (QuantLib::Dividend& childNative, Object^ owner);
        CDividend (CDividend^ copy);
        CDividend (System::Type^ t);
#ifdef STRUCT
        CDividend (QuantLib::Dividend childNative);
#endif       
#ifdef HANDLE
		CDividend (QuantLib::Handle<QuantLib::Dividend>& childNative, Object^ owner);
		CDividend (QuantLib::Handle<QuantLib::Dividend> childNative);
#endif
		virtual ~CDividend ();
		!CDividend ();

	internal:
		QuantLib::Dividend& GetReference ();
		boost::shared_ptr<QuantLib::Dividend>& GetShared ();
		QuantLib::Dividend* GetPointer ();
        void SetDividend (boost::shared_ptr<QuantLib::Dividend> native)
        {
            if (_ppDividend != NULL)
                delete _ppDividend;
            _ppDividend = new boost::shared_ptr<QuantLib::Dividend> (native);
            SetCashFlow (boost::dynamic_pointer_cast<QuantLib::CashFlow> (*_ppDividend));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Dividend>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Double Amount (Double underlying) ;
		virtual Double Amount () ;
        property DateTime Date 
        {
		    virtual DateTime get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDividend Vector interface
	ref class CDividendVector : public System::MarshalByRefObject,  
							    public INativeVector<IDividend^>,
							    public INativeVector<Generic::ICell<IDividend^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Dividend>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Dividend>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Dividend>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CDividendVector ();
#ifdef STRUCT
		CDividendVector (std::vector<QuantLib::Dividend>& vec, Object^ owner);
		CDividendVector (std::vector<QuantLib::Dividend> vec);
#endif       
		CDividendVector (std::vector<boost::shared_ptr<QuantLib::Dividend>>& vec, Object^ owner);
#ifdef HANDLE
		CDividendVector (std::vector<QuantLib::Handle<QuantLib::Dividend>>& vec, Object^ owner);
#endif
		virtual ~CDividendVector ();
		!CDividendVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IDividend^ default[int] 
		{
			virtual IDividend^ get (int index) = INativeVector<IDividend^>::default::get;
			virtual void set (int index, IDividend^ value) = INativeVector<IDividend^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Cashflows::IDividend^>^ CItem[int] 
		{
			virtual Generic::ICell<IDividend^>^ get (int index) = INativeVector<Generic::ICell<IDividend^>^>::default::get;
			virtual void set (int index, Generic::ICell<IDividend^>^ value) = INativeVector<Generic::ICell<IDividend^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Dividend>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Dividend>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Dividend>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CDividend_Factory : public System::MarshalByRefObject,  public IDividend_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of IDividend, with event notification of changes </summary> */
        virtual Cephei::IVector<IDividend^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IDividend, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IDividend^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IDividend^>^ CreateVector(System::Collections::Generic::IEnumerable<IDividend^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IDividend^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IDividend^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Cashflows */}
